﻿// Decompiled with JetBrains decompiler
// Type: DnProcessSimulateCommands.ExportToWeb.CApExportToWebHtmlWriter
// Assembly: DnProcessSimulateCommands, Version=2402.0.0.890, Culture=neutral, PublicKeyToken=null
// MVID: A885D881-AEDF-447E-852A-6F13102E089C
// Assembly location: C:\Program Files\Tecnomatix_2402\eMPower\DotNetCommands\DnProcessSimulateCommands.dll

using EngineeringInternalExtension;
using Microsoft.Win32;
using System;
using System.Globalization;
using System.IO;
using System.IO.Compression;
using System.Text;
using Tecnomatix.Engineering;
using Tecnomatix.Engineering.PrivateImplementationDetails;


namespace DnProcessSimulateCommands.ExportToWeb;

public class CApExportToWebHtmlWriter
{
  private string m_filePath;
  private string m_folderPath;
  private string m_operation;
  private string m_templateHtml;
  private string m_animationFileName;
  private string m_animationFramesFileName;
  private const string OPTIONS_APPEARENCE = "\\Options\\Appearence";
  public bool m_isOnAutoTest;
  private const string CAPTION_PLATFORM_ROBOTEXPERT = "RobotExpert";
  private const string CAPTION_PLATFORM_PROCESS_SIMULATE = "Process Simulate";
  private const string CAPTION_PLATFORM_TEAMCENTER = "Process Simulate";

  internal string AnimationDataFileName
  {
    get => this.m_animationFileName;
    set => this.m_animationFileName = value;
  }

  internal string AnimationFramesFileName
  {
    get => this.m_animationFramesFileName;
    set => this.m_animationFramesFileName = value;
  }

  public CApExportToWebHtmlWriter(string selectedPath, string operation)
  {
    this.m_filePath = selectedPath;
    this.m_folderPath = Path.GetDirectoryName(this.m_filePath);
    this.m_operation = operation;
    this.m_templateHtml = ExportToWebHtmlTemplate.PSonWeb;
  }

  public void CreateHtmlFile()
  {
    try
    {
      StringBuilder template = new StringBuilder(this.m_templateHtml);
      this.FillLocalizationPlacemarks(template);
      this.FillDataPlacemarks(template);
      this.FillImages(template);
      this.FillCSS(template);
      this.FillJS(template);
      this.FillAnimation(template);
    }
    catch (Exception ex)
    {
    }
  }

  private void FillLocalizationPlacemarks(StringBuilder template)
  {
    string str;
    switch ((int) TxApplication.PlatformType)
    {
      case 0:
      case 4:
        str = "Process Simulate";
        break;
      case 3:
        str = "RobotExpert";
        break;
      default:
        str = "Process Simulate";
        break;
    }
    template.Replace("{[[CRsExportToWeb_localization_HTML_LANGUAGE]]}", ExportToWebStringTable.HTML_LANGUAGE);
    template.Replace("{[[CRsExportToWeb_localization_HTML_PAGE_TITLE]]}", str + ExportToWebStringTable.HTML_PAGE_TITLE);
    template.Replace("{[[CRsExportToWeb_localization_HTML_WEB_VIEW_CAPTION]]}", str + ExportToWebStringTable.HTML_WEB_VIEW_CAPTION);
    template.Replace("{[[CRsExportToWeb_localization_PAN]]}", ExportToWebStringTable.HTML_BUTTON_PAN);
    template.Replace("{[[CRsExportToWeb_localization_ROTATE]]}", ExportToWebStringTable.HTML_BUTTON_ROTATE);
    template.Replace("{[[CRsExportToWeb_localization_ZOOM_TO_FIT]]}", ExportToWebStringTable.HTML_BUTTON_ZOOM_TO_FIT);
    template.Replace("{[[CRsExportToWeb_localization_ZOOM]]}", ExportToWebStringTable.HTML_BUTTON_ZOOM);
    template.Replace("{[[CRsExportToWeb_localization_INFO_TOOLTIP]]}", ExportToWebStringTable.HTML_BUTTON_INFO_TOOLTIP);
    template.Replace("{[[CRsExportToWeb_localization_SCENE_INFORMATION]]}", ExportToWebStringTable.HTML_SCENE_INFORMATION);
    template.Replace("{[[CRsExportToWeb_localization_STUDY]]}", ExportToWebStringTable.HTML_INFO_STUDY);
    template.Replace("{[[CRsExportToWeb_localization_OPERATION]]}", ExportToWebStringTable.HTML_INFO_OPERATION);
    template.Replace("{[[CRsExportToWeb_localization_DATE]]}", ExportToWebStringTable.HTML_INFO_DATE);
    template.Replace("{[[CRsExportToWeb_localization_LOADING_3D_SCENE]]}", ExportToWebStringTable.HTML_LOADING_3D_SCENE);
    template.Replace("{[[CRsExportToWeb_localization_RESET_TOOLTIP]]}", ExportToWebStringTable.HTML_RESET_TOOLTIP);
    template.Replace("{[[CRsExportToWeb_localization_PLAY_TOOLTIP]]}", ExportToWebStringTable.HTML_PLAY_TOOLTIP);
    template.Replace("{[[CRsExportToWeb_WebGL_Error_Title]]}", ExportToWebStringTable.WEBGL_ERROR_TITLE);
    template.Replace("{[[CRsExportToWeb_WebGL_Error_Button_Close]]}", ExportToWebStringTable.WEBGL_ERROR_BUTTON_CLOSE);
  }

  private void FillDataPlacemarks(StringBuilder template)
  {
    string newValue1 = "NA";
    string newValue2 = !TxTcUtilities.IsTcAnyPlatform ? (TxApplication.ActiveDocument.CurrentStudy == null ? "NA" : TxApplication.ActiveDocument.CurrentStudy.Caption) : TxApplication.ActiveDocument.PhysicalRoot.Name;
    if (this.m_operation != null)
    {
      newValue1 = this.m_operation;
      template.Replace("{[[CRsExportToWeb_ShouldHideControls]]}", "false");
    }
    else
      template.Replace("{[[CRsExportToWeb_ShouldHideControls]]}", "true");
    string shortDatePattern = CultureInfo.CurrentCulture.DateTimeFormat.ShortDatePattern;
    DateTime dateTime = DateTime.Now;
    dateTime = dateTime.Date;
    string newValue3 = dateTime.ToString(shortDatePattern);
    template.Replace("{[[CRsExportToWeb_StudyName]]}", newValue2);
    template.Replace("{[[CRsExportToWeb_OpName]]}", newValue1);
    template.Replace("{[[CRsExportToWeb_Date]]}", newValue3);
  }

  private void FillImages(StringBuilder template)
  {
    template.Replace("{[[CRsExportToWeb_IMAGE_TecnomatixLogo]]}", ExportToWebHtmlTemplate.tecnomatix_logo_png);
    template.Replace("{[[CRsExportToWeb_IMAGE_SiemensLogo]]}", ExportToWebHtmlTemplate.siemens_logo_png);
    template.Replace("{[[CRsExportToWeb_ICON_pan]]}", ExportToWebHtmlTemplate.pan_icon);
    template.Replace("{[[CRsExportToWeb_ICON_rotate]]}", ExportToWebHtmlTemplate.rotate_icon);
    template.Replace("{[[CRsExportToWeb_ICON_fit]]}", ExportToWebHtmlTemplate.fit_icon);
    template.Replace("{[[CRsExportToWeb_ICON_zoom]]}", ExportToWebHtmlTemplate.zoom_icon);
    template.Replace("{[[CRsExportToWeb_ICON_open_information_panel]]}", ExportToWebHtmlTemplate.info_png);
    template.Replace("{[[CRsExportToWeb_ICON_close_information_panel]]}", ExportToWebHtmlTemplate.close_information_panel_icon);
    template.Replace("{[[CRsExportToWeb_ICON_rewind]]}", ExportToWebHtmlTemplate.rewind_icon);
    template.Replace("{[[CRsExportToWeb_ICON_forward]]}", ExportToWebHtmlTemplate.forward_icon);
    template.Replace("{[[CRsExportToWeb_ICON_end]]}", ExportToWebHtmlTemplate.end_icon);
  }

  private void FillCSS(StringBuilder template)
  {
    template.Replace("{[[CRsExportToWeb_CSS_BootstrapMin]]}", ExportToWebHtmlTemplate.bootstrap_min_css);
    template.Replace("{[[CRsExportToWeb_CSS_SiemensTheme]]}", ExportToWebHtmlTemplate.siemens_theme_css);
  }

  private void FillJS(StringBuilder template)
  {
    template.Replace("{[[CRsExportToWeb_JS_detectWebGLSupported]]}", ExportToWebHtmlTemplate.detectWebGLSupported_js);
    template.Replace("{[[CRsExportToWeb_WebGL_Error_Video_Card]]}", ExportToWebStringTable.WEBGL_ERROR_VIDEO_CARD_PROBLEM);
    template.Replace("{[[CRsExportToWeb_WebGL_Error_Browser]]}", ExportToWebStringTable.WEBGL_ERROR_BROWSER_NOT_SUPPORTED);
    template.Replace("{[[CRsExportToWeb_WebGL_Error_PS_Help_reference]]}", ExportToWebStringTable.WEBGL_ERROR_PS_HELP_REFERENCE);
    template.Replace("{[[CRsExportToWeb_JS_BootstrapMin]]}", ExportToWebHtmlTemplate.bootstrap_min_js);
    template.Replace("{[[CRsExportToWeb_JS_jQueryMin]]}", ExportToWebHtmlTemplate.jquery_min_js);
    template.Replace("{[[CRsExportToWeb_JS_TrackballControlsJS]]}", ExportToWebHtmlTemplate.TrackballControls_js);
    if (ExportToWebHtmlTemplate.three_min_js.Length == 0)
      template.Replace("{[[CRsExportToWeb_JS_ThreeJS]]}", ExportToWebHtmlTemplate.three_js);
    else
      template.Replace("{[[CRsExportToWeb_JS_ThreeJS]]}", ExportToWebHtmlTemplate.three_min_js);
    if (File.Exists(this.AnimationFramesFileName))
    {
      try
      {
        template.Replace("{[[CRsExportToWeb_JS_AnimationFrames]]}", File.ReadAllText(this.AnimationFramesFileName));
        if (!this.m_isOnAutoTest)
          File.Delete(this.AnimationFramesFileName);
      }
      catch (Exception ex)
      {
      }
    }
    else
      template.Replace("{[[CRsExportToWeb_JS_AnimationFrames]]}", "");
    string str = ExportToWebHtmlTemplate.main_js;
    try
    {
      string name = TxApplicationEx.ConfigurationPath + "\\Options\\Appearence";
      object obj = Registry.CurrentUser.OpenSubKey(name).GetValue("TopLeft", (object) "16777215");
      if (obj != null)
      {
        int result;
        if (int.TryParse(obj.ToString(), out result))
        {
          string newValue = "THREE.Fog(0x" + ((result & 16711680 /*0xFF0000*/) >> 16 /*0x10*/ | result & 65280 | (result & (int) byte.MaxValue) << 16 /*0x10*/).ToString("X");
          str = str.Replace("THREE.Fog(0xFFFFFF", newValue);
        }
      }
    }
    catch (Exception ex)
    {
    }
    TxVector txVector1 = TxVector.Subtract(TxApplication.ViewersManager.GraphicViewer.CurrentCamera.Position, TxApplication.ViewersManager.GraphicViewer.CurrentCamera.ReferencePoint);
    txVector1.Normalize();
    TxVector txVector2 = new TxVector(TxApplication.ViewersManager.GraphicViewer.CurrentCamera.UpVector);
    txVector2.Normalize();
    if (txVector1.X * txVector1.X + txVector1.Y * txVector1.Y < 0.01 && txVector2.X * txVector2.X + txVector2.Y * txVector2.Y < 0.01)
    {
      txVector2.Z = txVector2.Y;
      txVector2.Y = txVector1.Z;
    }
    string newValue1 = string.Format((IFormatProvider) CultureInfo.InvariantCulture, "cameraPosition = new THREE.Vector3({0},{1},{2})", (object) txVector1.X, (object) txVector1.Y, (object) txVector1.Z);
    string newValue2 = string.Format((IFormatProvider) CultureInfo.InvariantCulture, "cameraUp = new THREE.Vector3({0},{1},{2})", (object) txVector2.X, (object) txVector2.Y, (object) txVector2.Z);
    string newValue3 = str.Replace("cameraPosition = new THREE.Vector3(0, 0, 0)", newValue1).Replace("cameraUp = new THREE.Vector3(0, 1, 0)", newValue2);
    template.Replace("{[[CRsExportToWeb_JS_main_body]]}", newValue3);
    template.Replace("{[[CRsExportToWeb_ICON_reset]]}", ExportToWebHtmlTemplate.reset_png);
    template.Replace("{[[CRsExportToWeb_ICON_pause]]}", ExportToWebHtmlTemplate.pause_icon);
    template.Replace("{[[CRsExportToWeb_ICON_play]]}", ExportToWebHtmlTemplate.play_icon);
  }

  private static byte[] CompressString(string inputData)
  {
    using (MemoryStream memoryStream1 = new MemoryStream(Encoding.ASCII.GetBytes(inputData)))
    {
      using (MemoryStream memoryStream2 = new MemoryStream())
      {
        using (DeflateStream destination = new DeflateStream((Stream) memoryStream2, CompressionMode.Compress))
        {
          memoryStream1.CopyTo((Stream) destination);
          destination.Close();
          return memoryStream2.ToArray();
        }
      }
    }
  }

  private void FillAnimation(StringBuilder template)
  {
    bool flag = false;
    if (File.Exists(this.AnimationDataFileName))
    {
      string str = template.ToString();
      int startIndex = str.IndexOf("{[[CRsExportToWeb_AnimationData]]}");
      if (startIndex >= 0 && startIndex < str.Length)
      {
        string contents1 = str.Remove(startIndex, str.Length - startIndex);
        string contents2 = str.Remove(0, startIndex + "{[[CRsExportToWeb_AnimationData]]}".Length);
        File.WriteAllText(this.m_filePath, contents1);
        File.AppendAllLines(this.m_filePath, File.ReadLines(this.AnimationDataFileName));
        File.AppendAllText(this.m_filePath, contents2);
        flag = true;
      }
      try
      {
        if (!this.m_isOnAutoTest)
          File.Delete(this.AnimationDataFileName);
      }
      catch (Exception ex)
      {
      }
    }
    if (flag)
      return;
    template.Replace("{[[CRsExportToWeb_AnimationData]]}", "");
    File.WriteAllText(this.m_filePath, template.ToString());
  }
}
